const formatOnlineDoc = doc => ({
  id: doc.id,
  title: doc.title,
  desc: doc.description,
  tags: doc.tag_titles || [],
  content: doc.content,

  created_at: doc.created_at,
  updated_at: doc.updated_at,
});

const formatAttach = attach => ({
  id: attach.id,
  title: attach.title,
  key: attach.ufile_key,

  created_at: attach.created_at,
  updated_at: attach.updated_at,
});

const formatUpload = upload => ({
  id: upload.id,
  title: upload.title,
  key: upload.ufile_key,

  created_at: upload.created_at,
  updated_at: upload.updated_at,
});

const compare = (obj1, obj2) => {
  const val1 = new Date(obj1.updated_at).getTime();
  const val2 = new Date(obj2.updated_at).getTime();
  if (val1 < val2) return -1;
  else if (val1 > val2) return 1;
  return 0;
};

const formatOldResourceAttaches = jsonAttaches => Object.keys(jsonAttaches)
  .map((key) => {
    const attach = JSON.parse(jsonAttaches[key]);
    return {
      id: key.substr(key.lastIndexOf('_')),
      title: attach.title,
      key: attach.key,
    };
  });

const formatOldResource = (item) => {
  if (!item) return undefined;
  return {
    id: item.id,
    title: item.title,
    desc: item.description,
    tags: item.tag_titles || [],
    content: item.content,

    attaches: formatOldResourceAttaches(JSON.parse(item.ufile_keys || '')) || [],

    created_at: item.created_at,
    updated_at: item.updated_at,
  };
};

const formatNewResource = (items) => {
  const [doc, attaches, uploads] = [{}, [], []];
  items.forEach((item) => {
    if (item.class_type === 'ProductSnippet') Object.assign(doc, formatOnlineDoc(item));
    if (item.class_type === 'ProductDocument') attaches.push(formatAttach(item));
    if (item.class_type === 'ProductLocalFile') uploads.push(formatUpload(item));
  });
  return Object.assign(doc, {
    attaches: attaches.sort(compare),
    uploads: uploads.sort(compare),
  });
};

export const formatNormalContribution = contribution => ({
  id: contribution.id,
  title: contribution.title,
  desc: contribution.description,

  author: contribution.owner || {}, // id nickname avatar

  accepted: contribution.accept, // 是否已经被接受
  closed: contribution.close, // 是否已经被关闭

  oldResource: formatOldResource(contribution.old_product_file), // 当前版本（采纳后是被修改的版本，漏洞：关闭后呢）
  newResource: formatNewResource(contribution.pull_quest_items), // 修改后的版本

  created_at: contribution.created_at,
  updated_at: contribution.updated_at,
});

// 专门处理“修改上传资源”的贡献
export const formatEditUploadContribution = (contribution) => {
  const upload = contribution.pull_quest_items[0];
  return {
    id: contribution.id,
    title: contribution.title,
    desc: contribution.description,

    author: contribution.owner || {}, // id nickname avatar

    accepted: contribution.accept, // 是否已经被接受
    closed: contribution.close, // 是否已经被关闭

    oldResource: formatOldResource(contribution.old_product_file), // 当前版本（采纳后是被修改的版本，漏洞：关闭后呢）
    newResource: {
      id: upload.id,
      title: upload.title,
      tags: upload.tag_titles,

      created_at: upload.created_at,
      updated_at: upload.updated_at,
    },

    created_at: contribution.created_at,
    updated_at: contribution.updated_at,
  };
};

export const formatContribution = (contribution) => {
  const isEdit = !!contribution.old_product_file;
  // 不需要判断length，因为修改只有对单修改
  const isUpload = contribution.pull_quest_items[0].class_type === 'ProductLocalFile';
  if (isEdit && isUpload) return formatEditUploadContribution(contribution);
  return formatNormalContribution(contribution);
};

export const formatContributions = contributions => contributions.map(i => formatContribution(i));
